//入口文件
import Vue from "vue";

import axios from "axios";
Vue.prototype.$axios = axios;

//1.1导入路由的包
import VueRouter from "vue-router";
//1.2安装路由
Vue.use(VueRouter);

//注册vuex
import Vuex from "vuex";
Vue.use(Vuex);

var car = JSON.parse(localStorage.getItem("car") || "[]");

var store = new Vuex.Store({
  state: {
    //this.$store.state.***
    car: car //car数组中存放一些商品的的对象{id:商品的id，count：要购买的数量，price：商品的单价，selected：false}
  },
  mutations: {
    //this.$store.commit('方法名称'，'按需传递唯一的参数')
    addToCar(state, goodsinfo) {
      var flag = false;
      state.car.some(item => {
        if (item.id == goodsinfo.id) {
          item.count += parseInt(goodsinfo.count);
          flag = true;
          return true;
        }
      });
      if (!flag) {
        state.car.push(goodsinfo);
      }
      localStorage.setItem("car", JSON.stringify(state.car));
    },
    updateGoodsInfo(state, goodsinfo) {
      state.car.some(item => {
        if (item.id == goodsinfo.id) {
          item.count = parseInt(goodsinfo.count);
          return true;
        }
      });

      localStorage.setItem("car", JSON.stringify(state.car));
    },
    removeFormCar(state, id) {
      state.car.some((item, i) => {
        if (item.id == id) {
          state.car.splice(i, 1);
          return true;
        }
      });
      localStorage.setItem("car", JSON.stringify(state.car));
    },
    updateGoodsSelected(state, info) {
      state.car.some(item => {
        if (item.id == info.id) {
          item.selected = info.selected;
        }
      });
      localStorage.setItem("car", JSON.stringify(state.car));
    }
  },
  getters: {
    //this.$store.getters.***
    getAllCount(state) {
      var c = 0;
      state.car.forEach(item => {
        c += item.count;
      });
      return c;
    },
    getGoodsCount(state) {
      var o = [];
      state.car.forEach(item => {
        o[item.id] = item.count;
      });
      return o;
    },
    getGoodsSelected(state) {
      var o = {};
      state.car.forEach(item => {
        o[item.id] = item.selected;
      });
      return o;
    },
    getGoodsCountAndAmount(state) {
      var o = {
        count: 0,
        amount: 0
      };
      state.car.forEach(item => {
        if (item.selected) {
          o.count += item.count;
          o.amount += item.price * item.count;
        }
      });
      return o;
    }
  }
});

import moment from "moment";

Vue.filter("dateFormat", function (dataStr, pattern = "YYYY-MM-DD HH:mm:ss") {
  return moment(dataStr).format(pattern);
});

//2.1 导入vue-resource
import VueResource from "vue-resource";
//2.2 安装vue-resource
Vue.use(VueResource);
//设置请求的根路径
//Vue.http.options.root = 'https://www.apiopen.top';
//Vue.http.options.root = 'http://47.89.21.179:8080';
//全局设置post表单数据的组织形式 application/x-www-form-urlencoded
Vue.http.options.emulateJSON = true;
//导入mui的样式
import "./lib/mui/css/mui.min.css";
import "./lib/mui/css/icons-extra.css";

//按需导入mintui组件
// import { Header,Swipe, SwipeItem, Button, Lazyload } from 'mint-ui'
// Vue.component(Header.name, Header);
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
// Vue.use(Lazyload);

import MintUI from "mint-ui";
Vue.use(MintUI);
import "mint-ui/lib/style.css";

import VuePreview from "vue-preview";
Vue.use(VuePreview);

//导入app根组件
//1.3导入自己的router.js模块
import router from "./router";

import app from "./App.vue";

var vm = new Vue({
  el: "#app",
  render: c => c(app),
  router, //挂载路由对象到vm实例上
  store //挂载store
});